package com.bwie.goods.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.goods.vo.GoodsVo;
import com.bwie.miaosha.common.utils.PageUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.miaosha.common.utils.Query;

import com.bwie.goods.dao.GoodsDao;
import com.bwie.goods.entity.GoodsEntity;
import com.bwie.goods.service.GoodsService;


@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private StringRedisTemplate stringredisTemplate;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsEntity> page = new Page<>();
        String pagee = (String) params.get("page");
        String size = (String) params.get("limit");
        System.out.println(pagee);
        if (!StringUtils.isEmpty(pagee)) {
            String goodsList = stringredisTemplate.opsForValue().get("goodList" + pagee);
            System.out.println(goodsList);
            if (!StringUtils.isEmpty(goodsList)) {
                List<GoodsEntity> goodsEntities = JSONObject.parseObject(goodsList, new TypeReference<List<GoodsEntity>>() {
                });
                System.out.println("去redis里查");
                page.setRecords(goodsEntities);
            }else {
                //在redis中没有缓存 再去数据库进行查询
                System.out.println("去数据库里查");
                IPage<GoodsEntity> pageee = this.page(
                        new Query<GoodsEntity>().getPage(params),
                        new QueryWrapper<GoodsEntity>()
                );
                List<GoodsEntity> records = pageee.getRecords();
                //json对象转为json字符串
                String jsonString = JSONObject.toJSONString(records);
                //设置过期时间为30分钟
                stringredisTemplate.opsForValue().set("goodList"+pagee,jsonString,10, TimeUnit.MINUTES);
                page=pageee;
            }
        }
        return new PageUtils(page);
    }

    @Override
    public GoodsVo findGoodVoById(Long id) {
        return goodsDao.findGoodVoById(id);
    }

    @Override
    public GoodsVo findGoodsById(Long id) {
        return goodsDao.findGoodsById(id);
    }

    @Override
    public List<GoodsVo> findGoods() {
        String miaoshaList = stringredisTemplate.opsForValue().get("miaoshaList");
        if (miaoshaList!=null) {
            System.out.println("从redis中查");
            List<GoodsVo> goodsVos = JSONObject.parseObject(miaoshaList, new TypeReference<List<GoodsVo>>() {
            });
            System.out.println(goodsVos);
            return goodsVos;
        }else {
            //从数据库中查询
            System.out.println("从数据库中查询");
            List<GoodsVo> goods = goodsDao.findGoods();
            String jsonString = JSONObject.toJSONString(goods);
            stringredisTemplate.opsForValue().set("miaoshaList",jsonString,10,TimeUnit.MINUTES);
            return goods;
        }
    }

}